package com.ptt.sqm.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.FragmentManager.OnBackStackChangedListener;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.ptt.sqm.component.MenuList;
import com.ptt.sqm.util.AppUtil;
import com.ptt.sqm.util.WidgetUtil;
import com.ptt.sqmonmobile.R;

public class MainActivity extends DefaultActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

	private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
	/**
	 * Fragment managing the behaviors, interactions and presentation of the
	 * navigation drawer.
	 */
	private NavigationDrawerFragment mNavigationDrawerFragment;

	/**
	 * Used to store the last screen title. For use in
	 * {@link #restoreActionBar()}.
	 */
	private CharSequence mTitle;

	// private int savedPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
		mTitle = getTitle();

		// Set up the drawer.
		mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));		
	}

	@Override
	public void onNavigationDrawerItemSelected(int position) {
		// update the main content by replacing fragments
		FragmentManager fragmentManager = getFragmentManager();

		onSectionAttached(position);

		if (position == 0)
			fragmentManager.beginTransaction().replace(R.id.container, HomeSummaryAssessFragment.newInstance(position)).commit();

		else if (position == menuList.getPosFrm_ShowAssessData())
			fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.container, SearchAssessFragment.newInstance(position)).commit();

		else if (position == menuList.getPosFrm_AssessData()) {
			Fragment mapFragment = (Fragment) getFragmentManager().findFragmentById(R.id.map);
			if (mapFragment != null) {
				FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				fragmentTransaction.remove(mapFragment);
				fragmentTransaction.commit();
			}

			fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.container, MakeAssessFragment.newInstance(position, null, false)).commit();
		} else if (position == menuList.getPosFrm_StationInfo())
			fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.container, StationInformationFragment.newInstance(position)).commit();

		else if (position == menuList.getPosRpt_AssessComment())
			fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.container, ReportAssessFragment.newInstance(position, null)).commit();

		else if (position == menuList.getPosRpt_AssessPoint())
			fragmentManager.beginTransaction().addToBackStack(null).replace(R.id.container, ReportScoreFragment.newInstance(position, null)).commit();

		else if (position == menuList.getPosLogout())
			// Log out
			session.logoutUser();

	}

	public void onSectionAttached(int number) {
		if (number == 0)
			mTitle = getString(R.string.title_section0);

		else if (number == menuList.getPosFrm_ShowAssessData())
			mTitle = getString(R.string.title_section1);

		else if (number == menuList.getPosFrm_AssessData())
			mTitle = getString(R.string.title_section2);

		else if (number == menuList.getPosFrm_StationInfo())
			mTitle = getString(R.string.title_section3);

		else if (number == menuList.getPosRpt_AssessComment())
			mTitle = getString(R.string.title_section4);

		else if (number == menuList.getPosRpt_AssessPoint())
			mTitle = getString(R.string.title_section5);

		else if (number == menuList.getPosLogout())
			mTitle = getString(R.string.title_section6);

	}

	public void restoreActionBar() {
		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayShowTitleEnabled(true);
		actionBar.setTitle(mTitle);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (!mNavigationDrawerFragment.isDrawerOpen()) {
			// Only show items in the action bar relevant to this screen
			// if the drawer is not showing. Otherwise, let the drawer
			// decide what to show in the action bar.
			getMenuInflater().inflate(R.menu.main, menu);
			restoreActionBar();
			return true;
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_home) {
			onSectionAttached(0);
			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			FragmentManager fragmentManager = getFragmentManager();
			if (getFragmentManager().getBackStackEntryCount() != 0) {
				for (int index = fragmentManager.getBackStackEntryCount() - 1; index > -1; index--) {
					if (fragmentManager.getBackStackEntryAt(index).getName() != null) {
						String prevStack = fragmentManager.getBackStackEntryAt(index).getName();
						fragmentManager.popBackStack(prevStack, FragmentManager.POP_BACK_STACK_INCLUSIVE);
						break;
					}
				}

			}
		}
		return false;
	}
}
